package com.yxt.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.yxt.common.sys.StringUtil;
import com.yxt.common.ui.ActivityStackControlUtil;
import com.yxt.common.ui.NormalTableAdapter;
import com.yxt.common.ui.NormalTableAdapter.TableCell;
import com.yxt.common.ui.NormalTableAdapter.TableRow;
import com.yxt.exception.InternetException;

public class ProjectListActivity extends Activity {
	private static final int MENU_RIVER = Menu.FIRST+1;
	private static final int MENU_BXWZ = Menu.FIRST+2;
	private static final int MENU_BYHS = Menu.FIRST + 3;
	private static final int MENU_MORE = Menu.FIRST + 4;
	private static final int menu_download = Menu.FIRST + 5;
	
	private int cols = 2;
	private int rows = 6;
	private int height = 40;
	private int color = Color.WHITE;
	private TextView lblTitle;
	private ProgressDialog pd;
	final int MODE_DIALOG = 1;

	// 对话框的创建方法
	protected Dialog onCreateDialog(int id) {
		Dialog result = null;
		switch (id) {
		case MODE_DIALOG:// 地图模式选择对话框的初始化
			AlertDialog.Builder bl = new AlertDialog.Builder(this);
			bl.setTitle("请选择");// 设置标题
			bl.setItems(new String[] { "河流水系", "报汛站网", "暴雨洪水", "水库特性", "常用知识" },
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
								case 0://
									break;
								case 1:
									break;
							}
						}
					});
			result = bl.create();
			break;
		}
		return result;
	}
	
	private void initComponent(){
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);		
		lblTitle = (TextView)findViewById(R.id.lblTitle);	
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});
		modifyTitle(getString(R.string.menu_river));
	}
	
	private void modifyTitle(String title){
		fillProjectList();
		lblTitle.setText(title);			
		fillProjectList();
	}
	
	/**
	 * 创建底部菜单
	 * */
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_RIVER, 1, R.string.menu_river);
		menu.add(0, MENU_BXWZ, 2, R.string.menu_bxwz);
		menu.add(0, MENU_BYHS, 3, R.string.menu_byhs);
		menu.add(0, MENU_MORE, 4, R.string.menu_more);
		menu.add(0, menu_download, 5, R.string.menu_download);
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * 点击底部菜单项时的处理
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_RIVER:
				modifyTitle(getString(R.string.menu_river));
				return true;
			case MENU_BXWZ:
				modifyTitle(getString(R.string.menu_bxwz));
				return true;
			case MENU_BYHS:
				modifyTitle(getString(R.string.menu_byhs));
				return true;
			case MENU_MORE:	
				// 显示模式选择对话框
				showDialog(MODE_DIALOG);
				return true;
			case menu_download:	
				pd = ProgressDialog.show(ProjectListActivity.this, "工情信息", "数据下载中,请稍后....", true, true);
				new Thread(){
					public void run(){
						try {
							Thread.sleep(3000L);
						} catch (Exception e) {
							throw new InternetException("");
						}
						pd.dismiss();
					}
				}.start();
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void fillProjectList(){
		List<TableRow> table = new ArrayList<TableRow>();     
        int[] width = {250,50};        
        // 每行的数据  
        ItemOnClickListener listener = new ItemOnClickListener();
        for (int i = 1; i <= rows; i++) {
            // 每行的数据     
            TableCell[] cells = new TableCell[cols];
            int len = cells.length;
            for (int j = 0; j < len ; j++) {
            	if (j == len -1) {
            		cells[j] = new TableCell(R.drawable.expand, width[j], height, color, TableCell.IMAGE, listener); 
            	} else {
            		cells[j] = new TableCell(StringUtil.getRandomString(), width[j], height, color, TableCell.STRING, null); 
            	}
            } 
            table.add(new TableRow(cells));  
        }
        NormalTableAdapter tableAdapter = new NormalTableAdapter(this, table);   
        ListView lstWaterStation = (ListView)findViewById(R.id.lstProjectList);
        lstWaterStation.setAdapter(tableAdapter);
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.project_list);   //软件activity的布局	
		ActivityStackControlUtil.add(this);  
		initComponent();		
	}

	private class ItemOnClickListener implements OnClickListener{
		private Map<String,String> paramsters = new HashMap<String,String>();
		
		public ItemOnClickListener(Map<String,String> map){
			paramsters.putAll(map);
		}
		
		public ItemOnClickListener(){					
		}
		
		public void setParameters(Map<String,String> paramsters){
			this.paramsters = paramsters;
		}
		
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), ProjectDetailActivity.class);
			startActivity(intent);
		}
		
	}
	
	protected void onDestroy() {     
        super.onDestroy();
        ActivityStackControlUtil.remove(this);
	}
}